package com.son.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.son.hotel.common.ErrorCode;
import com.son.hotel.common.Result;
import com.son.hotel.domain.Customers;
import com.son.hotel.domain.Dishes;
import com.son.hotel.domain.Orders;
import com.son.hotel.execption.BusinessException;
import com.son.hotel.mapper.DishesMapper;
import com.son.hotel.response.OrderAccountResp;
import com.son.hotel.response.OrderAndDishResp;
import com.son.hotel.service.CustomersService;
import com.son.hotel.service.DishesService;
import com.son.hotel.service.OrdersService;
import com.son.hotel.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 孙一卓
* @description 针对表【orders(菜品订单表)】的数据库操作Service实现
* @createDate 2025-06-24 15:11:35
*/
@Slf4j
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private DishesMapper dishesMapper;

    @Autowired
    private DishesService dishesService;

    @Autowired
    private CustomersService customersService;
    @Override
    public List<OrderAndDishResp> getOrder(int customerId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_Id",customerId);
        //查询订单信息
        //List<Orders> orders = ordersMapper.selectList(queryWrapper);
        List<Orders> orders = this.list(queryWrapper);
        Customers customers = customersService.getById(customerId);
        String name = customers.getName();
        if (CollectionUtil.isEmpty(orders)){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"查询失败");
        }
        ArrayList<OrderAndDishResp> list = new ArrayList<>();
        for (Orders order : orders) {
            Integer dishId = order.getDishId();
            //TODO 根据菜品id查询菜品名称
            Dishes dishes = dishesMapper.selectById(dishId);
            //获取菜品名称
            String dishesName = dishes.getName();
            OrderAndDishResp orderAndDishResp = new OrderAndDishResp();
            orderAndDishResp.setDishName(dishesName);
            orderAndDishResp.setName(name);
            orderAndDishResp.setOrders(order);
            list.add(orderAndDishResp);
        }
        return list;
    }

    @Override
    public List<OrderAccountResp> listOrderAccountTop5() {
        List<OrderAccountResp> list = new ArrayList<>();
        List<Orders> orders = ordersMapper.listOrderAccountTop5();
        log.info("order:{}",orders);
        for (Orders order : orders) {
            OrderAccountResp orderAccountResp = new OrderAccountResp();
            Integer dishId = order.getDishId();
            Integer quantity = order.getQuantity();
            Dishes dishes = dishesService.getById(dishId);
            String dishName = dishes.getName();

            orderAccountResp.setDishId(dishId);
            orderAccountResp.setDishName(dishName);
            orderAccountResp.setQuantity(quantity);

            list.add(orderAccountResp);
        }
        log.info("list:{}",list);
        return list;
    }
}




